package com.vvlive.caibao.smartcity.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioGroup;

import com.vvlive.caibao.smartcity.base.Optional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhj on 2015/11/26.
 * 投票选项
 */
public class VoteOptionsView extends LinearLayout {

    /**
     * 单选模式
     */
    public static final int SINGLE_MODE = 1;
    /**
     * 多选模式
     */
    public static final int MULTI_MODE = 2;

    /**
     * 默认是单选模式
     */
    private int mChoiceMode = SINGLE_MODE;

    private RadioGroup mGroup;

    public VoteOptionsView(Context context) {
        this(context, null);
    }

    public VoteOptionsView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mGroup = new RadioGroup(context);
        mGroup.setLayoutParams(new RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                                                  ViewGroup.LayoutParams.WRAP_CONTENT));
    }

    /**
     * 设置选择模式
     *
     * @param mode 选择模式
     */
    public void setChoiceMode(int mode) {
        mChoiceMode = mode;
    }

    /**
     * 添加一个选项
     *
     * @param option
     */
    public void addOption(String option) {
        Optional optional = null;
        if (mChoiceMode == SINGLE_MODE) {
            optional = new SingleOption(getContext());
        } else if (mChoiceMode == MULTI_MODE) {
            optional = new MultiOption(getContext());
        }
        if (optional != null) {
            optional.setQuestion(option);
        }
        mGroup.addView((CompoundButton) optional);
    }

    /**
     * 获取被选择的选项
     *
     * @return
     */
    public List<Integer> getChosenOption() {
        List<Integer> options = new ArrayList<>();
        for (int i = 0, count = mGroup.getChildCount(); i < count; i++) {
            if (((CompoundButton) mGroup.getChildAt(i)).isChecked()) {
                options.add(i);
            }
        }
        return options;
    }

}
